{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 设计类"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#设计一个类（类比生活中设计一张表格）\n",
    "class student:\n",
    "    name = None\n",
    "    age = None\n",
    "    genger = None\n",
    "    score = None\n",
    "#创建一个对象（类比生活中打印一张表格）\n",
    "s1 = student()\n",
    "#对对象属性进行赋值（类比生活中填写一张表格）\n",
    "s1.name = \"张三\"\n",
    "s1.age = 18\n",
    "s1.genger = \"男\"\n",
    "s1.score = 100\n",
    "#输出信息（查看表格）\n",
    "print(s1.name,s1.age,s1.genger,s1.score)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 类的成员方法\n",
    "面向对象编程的思想：先构建一个类，再根据类创建对象，对象去干活"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    name = None\n",
    "    def sayhi(self,msg):\n",
    "        print(f\"hello,i'm:{self.name},{msg}\")#必须要有self这个参数，负责方法无法访问类的成员变量\n",
    "\n",
    "s1 = student()\n",
    "s1.name = \"张三\"\n",
    "s1.sayhi(\"nice to meet you\")#传递参数self相当于不存在，不需要传这个参数\n",
    "s2 = student()\n",
    "s2.name = \"李四\"\n",
    "s2.sayhi(\"nice to meet you too\")\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 构造方法（类似c++构造函数）\n",
    "在构造类时会自动执行构造方法，并且构建类对象的传参会传递给构造方法，从而给成员变量赋值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    name = None\n",
    "    age = None\n",
    "    score = None\n",
    "\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "\n",
    "s1 = student(\"张三\", 18, 100)\n",
    "print(s1.name, s1.age, s1.score)\n",
    "s2 = student(\"李四\", 19, 99)\n",
    "print(s2.name, s2.age, s2.score)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 魔术方法\n",
    "__str__"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "    def __str__(self):#通过str魔术方法控制他的返回值\n",
    "        return f\"student类对象,姓名：{self.name} ,年龄：{self.age} ,成绩：{self.score}\"\n",
    "\n",
    "stu = student(\"张三\", 18, 100)\n",
    "print(stu)\n",
    "print(str(stu))\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## __lt__表示两个类的属性大小关系（>or<）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "    def __lt__(self, other):\n",
    "        return self.score < other.score\n",
    "\n",
    "stu1 = student(\"张三\", 18, 100)\n",
    "stu2 = student(\"李四\", 19, 99)\n",
    "\n",
    "print(stu1 > stu2)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## __le__ 魔术方法（加入等于）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "    def __le__(self, other):\n",
    "        return self.score <= other.score\n",
    "\n",
    "stu1 = student(\"张三\", 18, 100)\n",
    "stu2 = student(\"李四\", 19, 99)\n",
    "\n",
    "print(stu1 >= stu2)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## __eq__ 判断等于方法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return self.score == other.score\n",
    "\n",
    "stu1 = student(\"张三\", 18, 100)\n",
    "stu2 = student(\"李四\", 19, 99)\n",
    "\n",
    "print(stu1 == stu2)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 私有函数和私有方法外部对象无法使用，只有类内部可以使用（这些接口只用于内部工作，不向外公开）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class phone:\n",
    "    __current_voltage = 10\n",
    "    def __keep_single_core(self):\n",
    "        print(\"keep single core\")\n",
    "    def call_by5g(self):\n",
    "        if self.__current_voltage > 5:\n",
    "            print(\"call by 5g\")\n",
    "        else:\n",
    "            self.__keep_single_core()\n",
    "\n",
    "p = phone()\n",
    "p.call_by5g()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 继承"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class phone:\n",
    "    IMEI = 123\n",
    "    purduce = 'hw'\n",
    "    def call_4g(self    ):\n",
    "        print(\"4g通信\")\n",
    "class phone_2022(phone):\n",
    "    def call_5g(self):\n",
    "        print(\"5g通信\")\n",
    "p=phone_2022()\n",
    "p.call_5g()\n",
    "p.call_4g()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多继承,继承顺序从左到右依次降低（处理同名属性和同名方法）\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class phone:\n",
    "    IMEI = 123\n",
    "    purduce = 'hw'\n",
    "    def call_4g(self    ):\n",
    "        print(\"4g通信\")\n",
    "class phone_2022(phone):\n",
    "    def call_5g(self):\n",
    "        print(\"5g通信\")\n",
    "p=phone_2022()\n",
    "p.call_5g()\n",
    "p.call_4g()\n",
    "class NFCReader:\n",
    "    def read(self):\n",
    "        print(\"读卡\")\n",
    "    def write(self):\n",
    "        print(\"写卡\")\n",
    "class control:\n",
    "    rc_type=\"红外遥控\"\n",
    "    def control(self):\n",
    "        print(\"红外遥控开启\")\n",
    "class myphone(phone,NFCReader,control):\n",
    "    pass#pass用于继承后不添加新内容\n",
    "myphone1=myphone()\n",
    "myphone1.control()\n",
    "myphone1.read()\n",
    "myphone1.write()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 复写：子类继承父类的成员属性和成员方法后，如果对其不满意，可以进行复写\n",
    "## 复写后调用父类成员：1.使用成员变量：父类名.成员变量/成员方法  2.使用super（）：super（）。成员变量/成员函数\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4g通信++\n",
      "dw\n"
     ]
    }
   ],
   "source": [
    "class phone:\n",
    "    imei= None\n",
    "    producer = 'hw'\n",
    "    def call_4g(self):\n",
    "        print('4g通信')\n",
    "\n",
    "class phone_2022(phone):\n",
    "    producer = \"dw\"\n",
    "    def call_4g(self):\n",
    "        print(\"4g通信++\")\n",
    "\n",
    "p = phone_2022()\n",
    "p.call_4g()\n",
    "print(p.producer)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-26T11:57:38.766983500Z",
     "start_time": "2024-09-26T11:57:38.721605100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 一般能一眼看出来的不需要类型注解，一眼看不出来的加类型注解（比如一些函数的返回值）\n",
    "== 注== 函数注解不是强制性是注释性质的"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#变量的类型注解\n",
    "#基础类型注解\n",
    "from random import random\n",
    "\n",
    "var_1: int = 10\n",
    "var_2: str = \"hello\"\n",
    "var_3: bool = True\n",
    "var_4: float = 10.0\n",
    "#类对象类型注解\n",
    "class Student:\n",
    "    pass\n",
    "stu: Student = Student()\n",
    "#基础容器类型注解\n",
    "list_1: list = [1,2,3]\n",
    "set_1: set = {1,2,3}\n",
    "dict_1: dict = {\"name\": \"张三\", \"age\": 18}\n",
    "#基础类型详细注解\n",
    "list_1: list[int] = [1,2,3]\n",
    "set_1: set[int] = {1,2,3}\n",
    "dict_1: dict[str, int] = {\"name\": \"张三\", \"age\": 18}\n",
    "#在注释中进行类型注解\n",
    "var_1 = 10 #type: int\n",
    "var_2 = \"hello\" #type: str\n",
    "var_3 = True #type: bool\n",
    "var_4 = 10.0 #type: float\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多态：多种状态，完成某个行为时使用不同对象会得到不同状态。多态是一种动作函数，他的参数是类，执行的是父类的抽象功能\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wang\n",
      "miao\n"
     ]
    }
   ],
   "source": [
    "#多态\n",
    "class Animal:\n",
    "    def speak(self):\n",
    "        print(\"speak\")\n",
    "class cat(Animal):\n",
    "    def speak(self):\n",
    "        print(\"miao\")\n",
    "class dog(Animal):\n",
    "    def speak(self):\n",
    "        print(\"wang\")\n",
    "\n",
    "def speak(animal):\n",
    "    animal.speak()\n",
    "\n",
    "dog = dog()\n",
    "cat = cat()\n",
    "speak(dog)\n",
    "speak(cat)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-26T12:38:20.524654300Z",
     "start_time": "2024-09-26T12:38:20.506715600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##  抽象类，不定义父类函数中的函数功能，只定义接口，在子类函数中实现-"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mcool_wind\n",
      "Gcool_wind\n"
     ]
    }
   ],
   "source": [
    "#抽象类\n",
    "class Ac:\n",
    "    def cool_wind(self  ):\n",
    "        pass\n",
    "\n",
    "    def hot_wind(self):\n",
    "        pass\n",
    "    def swing_l_r(self  ):\n",
    "        pass\n",
    "class Midea(Ac):#美的空调\n",
    "    def cool_wind(self):\n",
    "        print(\"Mcool_wind\")\n",
    "    def hot_wind(self):\n",
    "        print(\"Mhot_wind\")\n",
    "    def swing_l_r(self):\n",
    "        print(\"Mswing_l_r\")\n",
    "class GeLi(Ac):#格力空调\n",
    "    def cool_wind(self):\n",
    "        print(\"Gcool_wind\")\n",
    "    def hot_wind(self):\n",
    "        print(\"Ghot_wind\")\n",
    "    def swing_l_r(self):\n",
    "        print(\"Gswing_l_r\")\n",
    "def make_cool(ac):\n",
    "    ac.cool_wind()\n",
    "m = Midea()\n",
    "g= GeLi()\n",
    "make_cool(m)\n",
    "make_cool(g)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-26T12:37:11.267697400Z",
     "start_time": "2024-09-26T12:37:11.222991700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
